<?php
include './c/config.php';
include './session/mysql.sessions.php';
$session = new Session();

$zslink='';
if (isset($_GET['zslink'])){
    $zslink=$_GET['zslink'];
}

$_SESSION['rand']=rand(1,9);
if (isset($_GET['op'])){
    $openid=$_GET['op'];
    $_SESSION['openid']=$openid;
    if ('localhost' != $_SERVER['SERVER_NAME']) {
        header('Location: http://climb-game.51wom.com/?f='.$openid);
        exit;
    }
}
if (isset($_GET['friend'])){
    $openidfriend=$_GET['friend'];
    $_SESSION['friendOp']=$openidfriend;
}
if (isset($_SESSION['openid']) && !empty($_SESSION['openid']) ){
    ini_set("display_errors", "On");
    error_reporting(E_ALL | E_STRICT);
    $mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
    $openid=$_SESSION['openid'];
    if (!empty($zslink) && !empty($openid)) { //统计组数据
        $sql="select id from `zsu_user_log` where openid='".$openid."'  limit 0,1 ";
        $query = $mysqli->query($sql);
        if($query->num_rows>0){

        }else{
        $sql="INSERT INTO `zsu_user_log` (`id`, `openid`, `zslink`,`ji`, `createtime`) VALUES (NULL, '".$openid."', '".$zslink."','1', '".date("Y-m-d H:i:s")."')";
        $mysqli->query($sql);
        }
        $query=NULL;
    }

    if (isset($_GET['f']) && !empty($_GET['f']) && $_GET['f'] != $openid){ //好友分享，增加玩次数
            $sql="select id from `zsu_user_log` where openid='".$openid."'  limit 0,1 ";
            $query = $mysqli->query($sql);
            if($query->num_rows>0){ }else{
                 $queryLieBian=$mysqli->query("select ji,zslink from zsu_user_log where openid='".$_GET['f']."' limit 0, 1");
                 if($queryLieBian->num_rows>0){
                    $getLieBian=$queryLieBian->fetch_assoc();
                    $ji=3;
                    if(1 == $getLieBian['ji']){
                        $ji=2;
                    }
                    if(2 == $getLieBian['ji']){
                        $ji=3;
                    }
                    $sql="INSERT INTO `zsu_user_log` (`id`, `openid`, `zslink`,`ji`, `createtime`) VALUES (NULL, '".$openid."', '".$getLieBian['zslink']."',".$ji.", '".date("Y-m-d H:i:s")."')";
                    $mysqli->query($sql);

                 }else{
                      $queryG=$mysqli->query("select id from zuansu_user where openid='".$openid."'  limit 0, 1");
                      if($queryG->num_rows>0){
                         $getD=$queryG->fetch_assoc();
                         $uid=$getD['id'];
                         $sql="INSERT INTO `zsu_user_log` (`id`, `openid`, `zslink`,`ji`, `createtime`) VALUES (NULL, '".$openid."', '".$uid."','1', '".date("Y-m-d H:i:s")."')";
                         $mysqli->query($sql);
                      }


                 }
            }
 
       header('Location: http://climb-game.51wom.com/?f='.$_SESSION['openid']);
       exit;    
    }
    
    if (!isset($_GET['f'])){
        if ('localhost' != $_SERVER['SERVER_NAME']) {
            header('Location: http://climb-game.51wom.com/?f='.$_SESSION['openid']);
            exit;
        }
    }    
    $sql="select phone,nickname,headimg from ".$tableVer."_users where openid='".$openid."'  limit 0,1 ";
    $query = $mysqli->query($sql);
    $getUserInfo=$query->fetch_assoc();
    $avatar=$getUserInfo['headimg'];
    $nickname=base64_decode($getUserInfo['nickname']);
    $phone=$getUserInfo['phone'];
    
    $userFen=0;
    $queryFen=$mysqli->query("select fen from ".$tableVer."_fen where openid='".$_SESSION['openid']."' limit 0, 1");
    if($queryFen->num_rows>0){
        $getFenInfo=$queryFen->fetch_assoc();
        $userFen=$getFenInfo['fen'];
    }
     
    $needAddZhandui='display:none;';
    $fTeamName='';


    if (isset($_GET['f']) && !empty($_GET['f']) && $_GET['f'] != $openid){ //好友分享，增加玩次数
         $queryFriend=$mysqli->query("select id from y1_users where openid='".$_GET['f']."' limit 0, 1");
         if($queryFriend->num_rows>0){
               $queryShare=$mysqli->query("select id from y1_share where woid='".$_GET['f']."' and fid='".$openid."' limit 0, 1");
               if($queryShare->num_rows>0){}else{
                
                $mysqli->query("INSERT INTO `y1_share` (`id`, `woid`, `fid`, `createtime`) VALUES (NULL,'".$_GET['f']."','".$openid."','".date("Y-m-d H:i:s")."')");
               }
         }

    }
 
    
    
    include 'jssdk.php';
    $jssdk = new JSSDK($appId, $secret);
    $signPackage = @$jssdk->GetSignPackage();
     
    if (empty($phone)) {
         include 'login.php';
     }else{
        include 'index-html.php';
     }

    
}else{
    $_SESSION['friendOp']='';
    if (isset($_SESSION['rand'])){
        header("Content-Type:text/html;charset=UTF-8");
        include './c/config.php';
        $f='';
        if (isset($_GET['f'])){
            $f=$_GET['f'];
            $_SESSION['friendOp']=$f;
        }
        $redirect_uri = urlencode('http://climb-game.51wom.com/b.php?f='.$f.'&zslink='.$zslink);
        $api_userinfo_base='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appId.'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_base#wechat_redirect';
        
        header('Location: '.$api_userinfo_base);
    }else{
        echo 'update your browser';
    }
}


?>